package org.ens.delegation.dao.impl;

import java.util.List;
import org.ens.delegation.bo.Grade;
import org.ens.delegation.dao.IGradeDao;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

public class GradeDao extends GenericDao<Grade, Integer> implements IGradeDao{

    @Override
    public Class<Grade> getReferencedClass() {
            return Grade.class;
    }

    @Override
    public List<Grade> findByFilters(String orderCol, String orderDir, String descriptionAr, String descriptionFr) {
                
        Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(getReferencedClass());
        
        Order o;
        if(orderDir.equals("asc"))
            o = Order.asc(orderCol);
        else o = Order.desc(orderCol);
        
        if(descriptionAr != null && !descriptionAr.isEmpty())
            criteria.add(Restrictions.like("descriptionAr", descriptionAr));
        
        if(descriptionFr != null && !descriptionFr.isEmpty())
            criteria.add(Restrictions.like("descriptionFr", descriptionFr));
               
        criteria.addOrder(o);
        return criteria.list();
    }

    @Override
    public List<Grade> findByFiltersWithFirstMaxResult(int first, int max, String orderCol, String orderDir, String descriptionAr, String descriptionFr) {
                
        Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(getReferencedClass());
        
        Order o;
        if(orderDir.equals("asc"))
            o = Order.asc(orderCol);
        else o = Order.desc(orderCol);
        
        if(descriptionAr != null && !descriptionAr.isEmpty())
            criteria.add(Restrictions.like("descriptionAr", descriptionAr));
        
        if(descriptionFr != null && !descriptionFr.isEmpty())
            criteria.add(Restrictions.like("descriptionFr", descriptionFr));
               
        criteria.setFirstResult(first);
        criteria.setMaxResults(max);
        criteria.addOrder(o);
        return criteria.list();
    }

    @Override
    public Integer getRowCountByFilters(String descriptionAr, String descriptionFr) {
                
        Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(getReferencedClass());
                
        if(descriptionAr != null && !descriptionAr.isEmpty())
            criteria.add(Restrictions.like("descriptionAr", descriptionAr));
        
        if(descriptionFr != null && !descriptionFr.isEmpty())
            criteria.add(Restrictions.like("descriptionFr", descriptionFr));

        return (Integer) criteria.uniqueResult();
    }

}
